package com.system.controller.admin;

import com.common.annotation.LogInfo;
import com.common.util.Excel.ExcelUtil;
import com.system.Excel.RoleExcel;
import com.system.Excel.RoleImport;
import com.system.domain.RoleUserListDto;
import com.system.dto.RoleDto;
import com.system.service.RoleExService;
import com.system.service.UserExService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/admin/role")
@Api(value = "后台角色控制层",tags = {"后台角色控制层"}) 
@LogInfo
@Primary
public class RoleExController {

    @Autowired
    private RoleExService roleService;
    @Autowired
    private UserExService userService;


    @ApiOperation("保存角色资源接口")
    @PostMapping("/saveRoleResource")
    @PreAuthorize("hasAnyAuthority(@servletUtils.getUrl)")
    public ResponseEntity saveRoleResource(@Valid @RequestBody RoleDto roleDto){return roleService.saveRoleResource(roleDto);}

    @ApiOperation("保存用户角色数据接口")
    @PostMapping("/saveRoleUserList")
    @PreAuthorize("hasAnyAuthority(@servletUtils.getUrl)")
    public ResponseEntity saveRoleUserList(@Valid @RequestBody RoleUserListDto roleUserListDto){return roleService.saveUserRole(roleUserListDto);}

    @ApiOperation("获取角色资源列表接口")
    @GetMapping("/getRoleResourceList/{roleId}")
    public ResponseEntity getRoleResourceList(@PathVariable("roleId") String roleId){return roleService.rolelistResource(roleId);}

    @ApiOperation("获取角色用户数据接口")
    @GetMapping("/getRoleUserList/{roleId}")
    public ResponseEntity getRoleUserList(@PathVariable("roleId") String roleId){return userService.getUserRoleData(roleId);}

    @ApiOperation("角色导出")
    @PostMapping("/export")
    @PreAuthorize("hasAnyAuthority(@servletUtils.getUrl)")
    public void  export(HttpServletResponse response){
        ExcelUtil.export(response, "角色数据", "角色数据表",  roleService.ExportList(), RoleExcel.class);
    }

    @PostMapping("/uploadExcel")
    @ApiOperation("角色文件信息上传接口")
    @PreAuthorize("hasAnyAuthority(@servletUtils.getUrl)")
    public ResponseEntity uploadExcel(@RequestParam("file") MultipartFile multipartFile) {
        RoleImport roleImport = new RoleImport(this.roleService, Boolean.valueOf(true));
        ExcelUtil.save(multipartFile,roleImport, RoleExcel.class);
        return ResponseEntity.ok("上传成功");
    }

    @PostMapping("/exportTemplate")
    @ApiOperation("导出角色模板接口")
    @PreAuthorize("hasAnyAuthority(@servletUtils.getUrl)")
    public void exportTemplate(HttpServletResponse response) {
        List<RoleExcel> list = new ArrayList<>();
        RoleExcel excel = new RoleExcel();
        excel.setId("xu0000xx");
        excel.setName("管理员");
        excel.setDesc("管理员");
        list.add(excel);
        ExcelUtil.export(response, "角色数据模板","角色数据模板表", list, RoleExcel.class);
    }


}
